<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :treeNode="company" :isRoot="true" @addDepts='addDepts'/>
        <!-- 放置一个tree -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts='getDepartments' @addDepts='addDepts' @editDepts='editDepts'/>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹层组件 -->
    <add-dept :show-dialog.sync='showDialog' :treeNode='node' @addDepts='getDepartments' ref="addDept"/>
  </div>
</template>

<script>
 // 导入树形组件
  import TreeTools from './components/tree-tools.vue'
  import { getDepartments } from '@/api/departments'
  // 引入转换树形结构的方法
  import { tranListToTreeData } from '@/utils'
  // 引入新增部门的组件
  import AddDept from './components/add-dept'
export default {
  components: {
    TreeTools,
    AddDept
  },
 data () {
    return {
      company: {},
       departs: [],
         defaultProps: {
          label: 'name'
        },
        // 控制弹层的显示与隐藏
        showDialog: false,
        node: null // 记录当前点击的node节点
    }
  },
   created () {
     this.getDepartments()
   },
   methods: {
    async getDepartments () {
       const res = await getDepartments()
       console.log(res)
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(res.depts, '') // 需要将其转化为树形结构
     },
    //  新增部门
     addDepts (node) {
      //  显示弹层
      this.showDialog = true
      this.node = node
     },
    //  编辑部门
    editDepts (node) {
      //  显示弹层
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
    }
   }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>